package com.example.mypplication.expendlist

import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.mypplication.R
import com.example.mypplication.databinding.ActivityFlexboxExpandBinding
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

class FlexboxExpandActivity : AppCompatActivity() {
    private lateinit var binding: ActivityFlexboxExpandBinding
    private lateinit var adapter: FlexboxExpandAdapter

    // 测试数据
    private val testItems = listOf(
        "短文本", "中等长度文本", "这是一个非常长的文本项会占据更多空间",
        "另一个", "项目", "不同长度", "多样", "内容", "用于演示",
        "Flexbox", "布局", "动态宽度", "元素", "测试", "数据",
        // 新增的40个不同长度数据
        "简", "稍长一些的文本", "这是一个中等长度的文本内容",
        "非常非常长的文本项，用于测试极端情况下的布局表现和文本换行效果",
        "Kotlin", "编程", "Android开发", "移动应用", "用户界面",
        "响应式设计", "前端技术", "后端服务", "数据库", "云计算",
        "人工智能", "机器学习", "深度学习", "自然语言处理", "计算机视觉",
        "短", "中", "长文本示例", "测试数据生成", "列表项目",
        "多样化的内容", "不同长度的字符串", "用于布局测试", "UI组件",
        "用户体验", "交互设计", "产品经理", "软件开发", "敏捷开发",
        "DevOps", "微服务", "容器化", "大数据", "物联网",
        "区块链", "网络安全", "隐私保护", "数字孪生", "元宇宙"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFlexboxExpandBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 配置参数
        val buttonWidth = resources.getDimensionPixelSize(R.dimen.expand_button_width)
        val horizontalSpacing = resources.getDimensionPixelSize(R.dimen.item_horizontal_spacing)
        val verticalSpacing = resources.getDimensionPixelSize(R.dimen.item_vertical_spacing)

        val recyclerViewWidth = resources.displayMetrics.widthPixels - dpToPx(this, 0F)

        // 初始化FlexboxLayoutManager
        val layoutManager = FlexboxLayoutManager(this).apply {
            flexDirection = FlexDirection.ROW
            flexWrap = FlexWrap.WRAP
            justifyContent = JustifyContent.FLEX_START
        }

        // 创建间距装饰
        val spacingDecoration = FlexboxSpacingDecoration(horizontalSpacing, verticalSpacing)

        binding.recyclerView.apply {
            this.layoutManager = layoutManager
            addItemDecoration(spacingDecoration)
            adapter = FlexboxExpandAdapter(
                this@FlexboxExpandActivity,
                items = testItems,
                buttonWidth = buttonWidth,
                horizontalSpacing = horizontalSpacing,
                recyclerViewWidth = recyclerViewWidth,
            ).also { this@FlexboxExpandActivity.adapter = it }
        }
    }

    fun dpToPx(context: Context, dpValue: Float): Int {
        // 获取设备的屏幕密度比例
        val scale = context.resources.displayMetrics.density
        // 四舍五入取整
        return (dpValue * scale + 0.5f).toInt()
    }

    // 自定义Flexbox间距装饰类
    class FlexboxSpacingDecoration(
        private val horizontalSpacing: Int,
        private val verticalSpacing: Int
    ) : RecyclerView.ItemDecoration() {

        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)

            val position = parent.getChildAdapterPosition(view)
            if (position == RecyclerView.NO_POSITION) return

            val adapter = parent.adapter ?: return

            // 设置横向间距（左右间距）
            outRect.left = horizontalSpacing / 2
            outRect.right = horizontalSpacing / 2

            // 设置纵向间距（上下间距）
            outRect.top = verticalSpacing / 2
            outRect.bottom = verticalSpacing / 2
        }
    }
}